/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.udt.generator.api.mgrinterface;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method.GetResInfoGenerator;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;
import com.inspur.edp.udt.core.Udt.UdtResInfoImpl;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;

public class UdtGetResInfoGenerator extends GetResInfoGenerator {

  public UdtGetResInfoGenerator(String fieldName, String nodeCode,
      EntityGeneratorContext parentContext) {
    super(fieldName, nodeCode, parentContext);
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo(UdtResInfoImpl.class);
  }

  @Override
  protected void buildThenStatement(IfStatement ifStatement) {
    Assignment assignment = this.ast.newAssignment();
    assignment.setOperator(org.eclipse.jdt.core.dom.Assignment.Operator.ASSIGN);
    assignment.setLeftHandSide(this.ast.newSimpleName(this.fieldName));
    ClassInstanceCreation creation = this.ast.newClassInstanceCreation();
    MediateType entityType = (MediateType)this.parentContext.getProjContext().getCefGenContext().getEntityResourceInfos().get(this.nodeCode);
    String typeName = entityType.getFullName();
    TypeInfo type = new TypeInfo(typeName);
    creation.setType(type.getType(this.ast, this.parentContext.getProjContext()));
    ClassInstanceCreation modelCreation = this.ast.newClassInstanceCreation();
    modelCreation.setType(this.ast.newSimpleType(this.ast.newName(((CefApiGeneratorContext)this.parentContext.getProjContext().getCefGenContext().getApiGenContext()).getMgrInfoType().getFullName())));
//    creation.arguments().add(modelCreation);
    assignment.setRightHandSide(creation);
    ExpressionStatement expressionStatement = this.ast.newExpressionStatement(assignment);
    ifStatement.setThenStatement(expressionStatement);
  }
}
